/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package jlxy.ise.eroad.service;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import jlxy.ise.eroad.controller.Bean.OrderBean;
import jlxy.ise.eroad.service.function.*;
import jlxy.ise.eroad.util.SqlOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author hcc25_000
 */
@Service
@Transactional
public class ClientService {
    @Autowired
    private SqlOperator sql;
    
    public Sret addClient(String username,String passwd){
        String stmt = "insert into client_info(username, passwd) values (?,?)";
        String id = this.sql.insert(stmt, username, passwd)[0];
        Sret sr = new Sret();
        sr.setOk();
        sr.setData(id);
        return sr;
    }
    
   public String searchClient(String username,String passwd){
       String result=null;
        String stmt = "select * from client_info where username=? and passwd=?";
        List list = sql.query(stmt, username, passwd);
        if(list.isEmpty()){
            result="error";
        }else{
            result="welcome";
        }
        return result;
   }
   
   public String getCDKey(OrderBean ob){
       String gaddress=ob.getGaddress();
       String paddress=ob.getGclient();
       String phonenum=ob.getPhonenum();
       int weight=ob.getWeight();
       String cdkey=ClientFunc.getCDKey();
       String gclient=ob.getGclient();
       String pclient=ob.getPclient();
       String car=ob.getCar();
       String stmt="insert into client_order(gaddress,paddress,phonenum,weight,cdkey,gclient,pclient,car) values(?,?,?,?,?,?,?,?)";
       this.sql.insert(stmt,gaddress,paddress,phonenum,weight,cdkey,gclient,pclient,car );
       return cdkey;
   }
   
   public List searchCDKey(String cdkey){
       List list=null;
       
       String stmt="select gaddress,paddress from client_order where cdkey=?";
       list=sql.query(stmt,cdkey);
       
       
       return list;//list中存放的是map
   }
   
   public List getInfo(String phonenum,String state){
       String stmt="select * from pack_info1 where phonenum=? and state=?";
       return sql.query(stmt, phonenum,state);
   }
   
   public Sret setPackageInfo(OrderBean ob){
       Sret sr=new Sret();
       String gaddress=ob.getGaddress();
       String paddress=ob.getGclient();
       String phonenum=ob.getPhonenum();
       int weight=ob.getWeight();
       String cdkey=ClientFunc.getCDKey1();//获取随机的订单号吗
       String gclient=ob.getGclient();
       String pclient=ob.getPclient();
       String car=ob.getCar();
       String time=ClientFunc.getTime();//获取下单的时间
       String stmt="insert into pack_info2(gaddress,paddress,phonenum,weight,cdkey,gclient,pclient,car,state,time) values(?,?,?,?,?,?,?,?,?,?)";
       this.sql.insert(stmt,gaddress,paddress,phonenum,weight,cdkey,gclient,pclient,car,"waiting",time );
       sr.setOk("货物编号");
       sr.setData(cdkey);
       
       return sr;
   }
   
   public List getPackageInfo(String phonenum){
       String stmt="select gaddress,paddress,cdkey,state,time from pack_info2 where phonenum=?";
       return this.sql.query(stmt, phonenum);
   }
}
